<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Created by PhpStorm.
 * The project “dev”
 * File name “Auth.php”
 *
 * User: znx
 * Company: 思议创想
 * Q  Q: 865303386
 * Site: www.siyi360.com
 *
 * Date: 2017-02-14
 * Time: 03:34
 */

/*
 * 管理系统用户登录认证类
 *
 * @author  思议迷往
 * */
class Admin_auth {

    /**
     * Codeigniter reference
     */
    private $CI;

    private $session_array_user;
    private $session_array_acl;

    function __construct()
    {
        // Get Codeigniter instance
        $this->CI =& get_instance();

        $this->CI->load->library('session');
        $this->CI->load->database();
        //$this->CI->load->helper('common_helper');

        //从session获取当前登录用户信息
        $this->session_array_user = $this->CI->session->user_info;

        //判断用户信息是否有效
        if ($this->session_array_user && is_array($this->session_array_user)) {
            //获取管理员角色名称
            $this->CI->db->select('role_name');
            $this->CI->db->where('role_id',$this->session_array_user['user_role']);
            $query = $this->CI->db->get('admin_role', 0, 1);
            $role_data = $query->result_array()[0];
            $this->session_array_user['user_role'] = $role_data['role_name'];

            //从session获取当前登录用户权限信息
            $this->session_array_acl = $this->CI->session->user_acl;
        }
    }

    /*
     * check_user_login
     * 验证用户是否登录
     *
     * @access public
     * @return boolean
     * */
    public function check_user_login()
    {
        $is_session = $this->CI->session->has_userdata('user_info');
        if ($is_session) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * get_user_info
     * 获取session中保存的用户信息
     *
     * @param string $session_key session键名
     * @return bool
     */
    public function get_user_info($session_key='')
    {
        if (!$this->check_user_login()) return false;

        if ($session_key) {
            $session_data = $this->session_array_user[$session_key];
        } else {
            $session_data = $this->session_array;
        }

        return $session_data;
    }

    /**
     * get_user_acl
     * 获取当前登录的系统用户的权限范围（控制器与操作方法）
     *
     * @return array|bool
     */
    public function get_user_acl()
    {
        $session_data = $this->session_array_acl;

        if (!is_array($session_data)) return false;

        $session_acl_arr = array();
        foreach ($session_data as $key=>$val)
        {
            $session_acl_arr[$val['acl_controller']] = str_replace('|', ',', $val['acl_action']);
        }

        return $session_acl_arr;
    }

    /**
     * check_auth
     * 权限验证
     *
     * @return bool
     */
    public function check_auth()
    {
        $session_data = $this->session_array_acl;
        $current_controller = $this->CI->router->fetch_class();
        $current_action = $this->CI->router->fetch_method();
        if (is_array($session_data)) {
            // 判断用户权限中是否有当前访问的控制器存在
            if (!array_key_exists($current_controller, $this->get_user_acl())) {
                return false;
            } else {
                // 判断用户权限中是否有当前访问的所属控制器的方法存在
                if (preg_match('#'.$current_action.'#', $this->get_user_acl()[$current_controller])) {
                    return true;
                } else {
                    return false;
                }
            }
        } else {
            if ($session_data=='all') { //超管
                return true;
            } else if ($session_data=='null') { //无任何权限
                return false;
            }
        }
    }
}